package com.yltrcc.blog.controller.api;

import cn.hutool.log.StaticLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yltrcc.blog.generate.mapper.QbCategoryMapper;
import com.yltrcc.blog.generate.service.QbCategoryService;
import com.yltrcc.blog.model.model.ApiResponse;
import com.yltrcc.blog.model.model.BaseApiResponse;
import com.yltrcc.blog.generate.domain.*;
import com.yltrcc.blog.business.QuestionCategoryService;
import com.yltrcc.blog.business.QuestionService;
import com.yltrcc.blog.model.enums.BlogEnums;
import com.yltrcc.blog.model.exception.CheckException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Package: com.yltrcc.blog.api
 * Date：2022-02-03
 * Time：13:21
 * Description：用户获取面试题相关API
 *
 * @author yltrcc
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/question")
public class QuestionApiController {

    /**
     * 日志
     **/
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionCategoryService questionCategoryService;

    @Resource
    private QbCategoryMapper qbCategoryMapper;


    @GetMapping("/getpage")
    public String getpage(@RequestParam("id") int id) {

        int count = questionService.getCounts();
        if (count == 0) {
            return "Sorry, 没有数据，请添加数据";
        }
        if (id > count) {
            return "Sorry, 当前数据库中没有此数据";
        }
        StringBuilder sb = new StringBuilder();
        Question question = questionService.findByQuestionId(id);
        sb.append(question.getArticleContent());
        sb.append("\n\n");

        return sb.toString();
    }

    @GetMapping("/getcount")
    public int getcount() {

        return questionService.getCounts();
    }

    @GetMapping("/getcategory")
    public ApiResponse<QuestionCategory> getcategory(Boolean isTop, String categoryId) {

        List<QuestionCategory> data = questionCategoryService.findCategory(isTop, null, categoryId);
        ApiResponse<QuestionCategory> response = new ApiResponse<>(data);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    @GetMapping("/queryByCategory")
    public ApiResponse<Question> queryByCategory(@RequestParam("categoryId") Long categoryId) {

        List<Question> data = questionService.findByCategory(categoryId);
        ApiResponse<Question> response = new ApiResponse<>(data);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    @GetMapping("/queryArticleCategoryByCategoryId")
    public ApiResponse<QuestionList> queryArticleCategoryByCategoryId(@RequestParam("categoryId") Integer categoryId) {

        List<QuestionList> res = new ArrayList<>();
        //分两步发送请求
        //第一步 通过分类id 获取子分类
        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(categoryId);
        //第二步 根据子分类 获取 文章
        if (!CollectionUtils.isEmpty(subCategories)) {
            for (SubCategory subCategory : subCategories) {
                QuestionList questionList = new QuestionList();
                questionList.setSubCategoryName(subCategory.getName());
                List<Question> data = questionService.findBySubCategory(subCategory.getId());
                questionList.setData(data);
                res.add(questionList);
            }
        }
        ApiResponse<QuestionList> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/queryCategoryV2ByCategoryId")
    public ApiResponse<QuestionV2List> queryCategoryV2ByCategoryId(@RequestParam("categoryId") Integer categoryId) {

        List<QuestionV2List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 通过分类id 获取下一级分类
        List<Category> categories = questionService.findLowerCategoryByCategoryId(categoryId);
        //第二步 根据下一级分类 获取 子分类和文章
        if (!CollectionUtils.isEmpty(categories)) {
            for (Category category : categories) {
                //第一步 通过分类id 获取子分类
                List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                //第二步 根据子分类 获取 文章
                List<QuestionList> res1 = new ArrayList<>();
                QuestionV2List questionV2List = new QuestionV2List();
                if (!CollectionUtils.isEmpty(subCategories)) {
                    for (SubCategory subCategory : subCategories) {
                        QuestionList questionList = new QuestionList();
                        questionList.setSubCategoryName(subCategory.getName());
                        List<Question> data = questionService.findBySubCategory(subCategory.getId());
                        questionList.setData(data);
                        res1.add(questionList);
                    }
                }
                questionV2List.setCategoryName(category.getCategoryName());
                questionV2List.setData(res1);
                res.add(questionV2List);
            }
        }
        ApiResponse<QuestionV2List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面 查询出所有数据
     */
    @GetMapping("/queryAllCategoryV2ByCategoryId")
    public ApiResponse<QuestionV2List> queryAllCategoryV2ByCategoryId() {

        List<QuestionV2List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 top 分类
        List<Category> topCategories = questionService.findCategoryByTop();
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                List<Question> data = questionService.findBySubCategory(subCategory.getId());
                                questionList.setData(data);
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(res1);
                        res.add(questionV2List);
                    }
                }
            }

        }

        ApiResponse<QuestionV2List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面 查询出所有数据
     */
    @GetMapping("/queryAllCategoryV3ByCategoryId")
    public ApiResponse<QuestionV3List> queryAllCategoryV3ByCategoryId() {

        List<QuestionV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 top 分类
        List<Category> topCategories = questionService.findCategoryByTop();
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                List<Question> data = questionService.findBySubCategory(subCategory.getId());
                                questionList.setData(data);
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }


    /**
     * 三级界面 查询出所有分类不包含面试题
     */
    @GetMapping("/queryAllCategoryV3")
    public ApiResponse<QuestionV3List> queryAllCategoryV3() {

        List<QuestionV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 top 分类
        List<Category> topCategories = questionService.findCategoryByTop();
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setCategoryId(category.getCategoryId());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setCategoryId(topCategory.getCategoryId());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }


    /**
     * 三级界面 查询出所有算法分类
     */
    @GetMapping("/queryAllAlgorithm")
    public ApiResponse<QuestionV3List> queryAllAlgorithm() {

        List<QuestionV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 top 分类
        List<Category> topCategories = questionService.findLowerCategoryByCategoryId(45);
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面 查询出所有小说分类
     */
    @GetMapping("/queryAllNovel")
    public ApiResponse<QuestionV3List> queryAllNovel() {

        List<QuestionV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 103 小说
        List<Category> topCategories = questionService.findLowerCategoryByCategoryId(103);
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面 查询出所有 - 仙路缥缈
     */
    @GetMapping("/queryAll104")
    public ApiResponse<QuestionV3List> queryAll104() {

        List<QuestionV3List> res = new ArrayList<>();
        //分两步发送请求
        //第一步 查询所有 仙路缥缈 小说
        List<Category> topCategories = questionService.findLowerCategoryByCategoryId(104);
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 三级界面 查询出所有 - 仙路缥缈
     */
    @GetMapping("/queryAllByUpperId")
    public ApiResponse<QuestionV3List> queryAllByUpperId(@RequestParam(value = "upperId", defaultValue = "0") Integer upperId) {

        List<QuestionV3List> res = new ArrayList<>();
        if (upperId == 0) {
            ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
            response.setStatus(200);
            response.setCode(200);
            response.setMessage("upperId 不能为0或者空！！");
        }

        //分两步发送请求
        //第一步 查询所有 仙路缥缈 小说
        List<Category> topCategories = questionService.findLowerCategoryByCategoryId(upperId);
        //第一步 通过top分类id 获取下一级分类
        if (!CollectionUtils.isEmpty(topCategories)) {
            for (Category topCategory : topCategories) {
                List<Category> categories = questionService.findLowerCategoryByCategoryId(topCategory.getCategoryId());
                //第三步 根据下一级分类 获取 子分类和文章
                List<QuestionV2List> v2Lists = new ArrayList<>();
                if (!CollectionUtils.isEmpty(categories)) {
                    for (Category category : categories) {
                        //第一步 通过分类id 获取子分类
                        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                        //第二步 根据子分类 获取 文章
                        List<QuestionList> res1 = new ArrayList<>();
                        QuestionV2List questionV2List = new QuestionV2List();
                        if (!CollectionUtils.isEmpty(subCategories)) {
                            for (SubCategory subCategory : subCategories) {
                                QuestionList questionList = new QuestionList();
                                questionList.setSubCategoryName(subCategory.getName());
                                questionList.setSubCategoryId(subCategory.getId());
                                res1.add(questionList);
                            }
                        }
                        questionV2List.setCategoryName(category.getCategoryName());
                        questionV2List.setCategoryId(category.getCategoryId());
                        questionV2List.setData(res1);
                        v2Lists.add(questionV2List);
                    }
                }
                QuestionV3List v3List = new QuestionV3List();
                v3List.setCategoryName(topCategory.getCategoryName());
                v3List.setCategoryId(topCategory.getCategoryId());
                v3List.setData(v2Lists);
                res.add(v3List);
            }

        }

        ApiResponse<QuestionV3List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 根据子分类查询 面试题
     *
     * @param subCategoryId 子分类
     * @return
     */
    @GetMapping("/queryQuestionBySubCategoryId")
    public ApiResponse<QuestionList> queryQuestionBySubCategoryId(@RequestParam("subCategoryId") Integer subCategoryId) {

        List<QuestionList> res = new ArrayList<>();
        QuestionList questionList = new QuestionList();
        List<Question> data = questionService.findBySubCategory(subCategoryId);
        questionList.setData(data);
        res.add(questionList);
        ApiResponse<QuestionList> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 根据子分类查询 面试题
     *
     * @return
     */
    @GetMapping("/queryQuestionIsProject")
    public ApiResponse<QbCategory> queryQuestionIsProject() {

        LambdaQueryWrapper<QbCategory> lambdaQueryWrapper = Wrappers.lambdaQuery(QbCategory.class)
                .eq(QbCategory::getIsProject, 1);
        List<QbCategory> qbCategories = qbCategoryMapper.selectList(lambdaQueryWrapper);
        ApiResponse<QbCategory> result = new ApiResponse<>(qbCategories);
        result.setStatus(200);
        result.setCode(200);
        result.setMessage("获取成功！！！");
        return result;
    }


    /**
     * 根据子分类查询 面试题
     *
     * @return
     */
    @GetMapping("/findSubCategoryByCategoryId")
    public ApiResponse<QbCategory> findSubCategoryByCategoryId(@RequestParam("categoryId") Integer categoryId) {

        List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(categoryId);
        List<QbCategory> categories = new ArrayList<>();
        subCategories.forEach(object -> {
            QbCategory tmp = new QbCategory();
            tmp.setCategoryId(object.getId());
            tmp.setCategoryName(object.getName());
            categories.add(tmp);
        });
        ApiResponse<QbCategory> result = new ApiResponse<>(categories);
        result.setStatus(200);
        result.setCode(200);
        result.setMessage("获取成功！！！");
        return result;
    }

    @GetMapping("/findQuestionBySubCategoryId")
    public ApiResponse<QbCategory> findQuestionBySubCategoryId(@RequestParam("subCategoryId") Integer subCategoryId) {

        List<Question> data = questionService.findBySubCategory(subCategoryId);
        List<QbCategory> categories = new ArrayList<>();
        data.forEach(object -> {
            QbCategory tmp = new QbCategory();
            tmp.setCategoryId(object.getId());
            tmp.setCategoryName(object.getArticleTitle());
            categories.add(tmp);
        });
        ApiResponse<QbCategory> response = new ApiResponse<>(categories);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    /**
     * 根据子分类查询 面试题
     *
     * @param categoryId 分类
     * @return
     */
    @GetMapping("/queryQuestionByCategoryId")
    public ApiResponse<QuestionV2List> queryQuestionByCategoryId(@RequestParam("categoryId") String categoryId) {

        List<QuestionV2List> res = new ArrayList<>();
        List<QuestionList> questionLists = new ArrayList<>();
        QuestionV2List questionV2List = new QuestionV2List();
        QuestionCategory questionCategory = questionCategoryService.findByCategoryId(categoryId);
        if (null == questionCategory) {
            res.add(questionV2List);
            ApiResponse<QuestionV2List> response = new ApiResponse<>(res);
            response.setStatus(200);
            response.setCode(200);
            response.setMessage("获取成功！！！");
            return response;
        }
        if (null != questionCategory.getIsFinal() && 1 == questionCategory.getIsFinal()) {
            //第一步 通过分类id 获取子分类
            List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(Integer.valueOf(categoryId));
            //第二步 根据子分类 获取 文章
            if (!CollectionUtils.isEmpty(subCategories)) {
                for (SubCategory subCategory : subCategories) {
                    List<Question> bySubCategory = questionService.findBySubCategory(subCategory.getId());
                    QuestionList questionList = new QuestionList();
                    questionList.setSubCategoryId(subCategory.getCategoryId());
                    questionList.setSubCategoryName(subCategory.getName());
                    questionList.setData(bySubCategory);
                    questionLists.add(questionList);
                }
            }
            questionV2List.setCategoryName(questionCategory.getCategoryName());
            questionV2List.setCategoryId(Math.toIntExact(questionCategory.getCategoryId()));
            questionV2List.setData(questionLists);
        } else {
            //第一步 通过分类id 获取子分类
            List<Category> categories = questionService.findLowerCategoryByCategoryId(Integer.valueOf(categoryId));
            //第三步 根据下一级分类 获取 子分类和文章
            if (!CollectionUtils.isEmpty(categories)) {
                for (Category category : categories) {
                    //第一步 通过分类id 获取子分类
                    List<SubCategory> subCategories = questionService.findSubCategoryByCategoryId(category.getCategoryId());
                    //第二步 根据子分类 获取 文章
                    if (!CollectionUtils.isEmpty(subCategories)) {
                        for (SubCategory subCategory : subCategories) {
                            List<Question> bySubCategory = questionService.findBySubCategory(subCategory.getId());
                            QuestionList questionList = new QuestionList();
                            questionList.setSubCategoryId(subCategory.getCategoryId());
                            questionList.setSubCategoryName(subCategory.getName());
                            questionList.setData(bySubCategory);
                            questionLists.add(questionList);
                        }
                    }
                }
            }
            questionV2List.setCategoryName(questionCategory.getCategoryName());
            questionV2List.setCategoryId(Math.toIntExact(questionCategory.getCategoryId()));
            questionV2List.setData(questionLists);
        }


        questionV2List.setData(questionLists);
        res.add(questionV2List);
        ApiResponse<QuestionV2List> response = new ApiResponse<>(res);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }


    @GetMapping("/getAppVersionCode")
    public ApiResponse<String> getAppVersionCode() throws IOException {

        StringBuilder sb = new StringBuilder();
        //搜索当前目录下的APK文件 算了还是用数据库来做吧
        String appPath = "usr/yltrcc/nginx/android";
        Path path = Paths.get(appPath);
        StaticLog.info("This is static {} log. " + Files.isDirectory(path), "INFO");
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
            for (Path entry : stream) {
                if (entry.toFile().isFile()) {
                    sb.append(entry.toFile().getName());
                }
            }
        }

        List<String> data = new ArrayList<>();
        data.add(sb.toString());
        ApiResponse<String> response = new ApiResponse<>(data);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    @GetMapping("/getCategoryByUpperCategoryId")
    public ApiResponse<QuestionCategory> getCategoryByUpperCategoryId(String upperCategoryId) {

        List<QuestionCategory> data = questionCategoryService.getCategoryByUpperCategoryId(upperCategoryId);
        ApiResponse<QuestionCategory> response = new ApiResponse<>(data);
        response.setStatus(200);
        response.setCode(200);
        response.setMessage("获取成功！！！");

        return response;
    }

    @PostMapping("/updateContent")
    public BaseApiResponse updateContent(@RequestParam(value = "id") String id,
                                         @RequestParam(value = "content", required = false) String content,
                                         @RequestParam(value = "title", required = false) String title,
                                         @RequestParam(value = "category", required = false) String category,
                                         @RequestParam(value = "subCategory", required = false) String subCategory,
                                         @RequestParam(value = "summary", required = false) String summary,
                                         @RequestParam(value = "sort", required = false) String sort) throws Exception {
        BaseApiResponse response = new BaseApiResponse();

        if (!StringUtils.isEmpty(id)) {
            log.info("id = " + id + ", content: " + content);
            Question question = questionService.selectByPrimaryKey(new Integer(id));
            if (!StringUtils.isEmpty(content)) {
                question.setArticleContentMd(content);
            }
            if (!StringUtils.isEmpty(title)) {
                question.setArticleTitle(title);
            }
            if (!StringUtils.isEmpty(category)) {
                question.setCategoryId(new Long(category));
            }
            if (!StringUtils.isEmpty(subCategory)) {
                question.setSubCategoryId(new Long(subCategory));
            }
            if (!StringUtils.isEmpty(summary)) {
                question.setArticleSummary(summary);
            }
            if (!StringUtils.isEmpty(sort)) {
                question.setSort(sort);
            }
            question.setArticleUpdatetime(new Date());
            questionService.update(question, null);
            response.setStatus(200);
            response.setCode(200);
            response.setMessage("更新成功！！！");
        } else {
            response.setStatus(400);
            response.setCode(400);
            response.setMessage("ID 为空，请检查！！");
        }


        return response;
    }

    @GetMapping("/queryById")
    public ApiResponse<Question> queryById(@RequestParam(value = "id") String id) {
        List<Question> questions = new ArrayList<>();
        ApiResponse<Question> response = new ApiResponse<>(questions);

        if (!StringUtils.isEmpty(id)) {
            Question question = questionService.selectByPrimaryKey(new Integer(id));
            response.setStatus(200);
            response.setCode(200);
            response.setMessage("获取成功！！！");
            questions.add(question);
            response.setData(questions);
        } else {
            response.setStatus(400);
            response.setCode(400);
            response.setMessage("ID 为空，请检查！！");
        }


        return response;
    }

    /**
     * 保存文章
     *
     * @param question 面试题
     * @param tags     标签
     * @return
     */
    @PostMapping(value = "/save")
    public BaseApiResponse save(Question question, Long[] tags, Long category, Long subCategory, HttpServletRequest request) {
        try {
            questionService.save(question, tags, category, subCategory, request, 9);
        } catch (CheckException e) {
            return new BaseApiResponse(400, 400, e.getMsgDes());
        } catch (Exception e) {
            log.error("添加或更新失败" + e.getMessage());
            return new BaseApiResponse(400, 400, BlogEnums.ERROR.getMessage());
        }
        return new BaseApiResponse(200, 200, BlogEnums.PRESERVE_SUCCESS.getMessage());
    }

}
